from django import template
from django.utils.safestring import mark_safe

register = template.Library()


@register.filter("my_sub")
def sub(value, val):
    return value - val


@register.filter
def do_nothing(value):
    return value


# @register.simple_tag(takes_context=True)
# def get_ip(context, value):
#     for c in context:
#         if "request" in c:
#             return c['request'].META["REMOTE_ADDR"]
#     return value

class IPNode(template.Node):
    def __init__(self, arg):
        super(IPNode, self).__init__()
        self.argument = arg

    def render(self, context):
        for c in context:
            if "request" in c:
                return c['request'].META["REMOTE_ADDR"]

        return self.argument


def do_get_ip(parser, token):
    tag, arg = token.split_contents()

    return IPNode(arg)

register.tag("get_ip", do_get_ip)